package com.ooc.formV1.service.impl;

import java.io.PrintWriter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ooc.formV1.dao.impl.UserDaoImpl;
import com.ooc.formV1.service.UserService;
import com.oocl.formV1.dao.UserDao;
import com.oocl.formV1.pojo.User;

public class UserServiceImpl implements UserService{

	private UserDao userDao;
	
	public UserServiceImpl() {
		userDao = new UserDaoImpl();
	}

	@Override
	public int addUser(User user) {
		return userDao.addUser(user);
	}

	@Override
	public List<User> findAll() {
		return userDao.findAll();
	}

	@Override
	public boolean verifyFormat(User user) {
		String nameRegex = "^(([\u4e00-\u9fa5]{2,8})|([a-zA-Z]{2,16}))$";
		Pattern p = Pattern.compile(nameRegex);
		Matcher m = p.matcher(user.getName());
		if(m.matches()){
			return true;
		}
		return false;
	}

	@Override
	public void showAll(List<User> users, PrintWriter writer) {
		
		for(User u:users){
			writer.write(u.toString());
			writer.write("</br>");
		}
	}
	
	
}
